<?php

/**
 * 组合多维数组
 * @param $cate
 * @param string $name
 * @param int $pid
 * @return array
 */
function unlimitedForLayer($cate, $name = 'sub', $pid = 0)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $v[$name] = unlimitedForLayer($cate, $name, $v['id']);
            $v['url'] = get_url_list($v);
            $arr[] = $v;
        }
    }
    return $arr;
}


/**
 * 传递一个父级分类ID返回当前子分类
 * @param $cate
 * @param $pid
 * @return array
 */
function get_childs_on($cate, $pid)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $v['sub'] = get_childs($cate, $v['id']);
            $v['url'] = get_url_list($v);
            $arr[] = $v;
        }
    }
    return $arr;
}

/**
 * 传递一个父级分类ID返回所有子分类
 * @param $cate
 * @param $pid
 * @return array
 */
function get_childs($cate, $pid)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['pid'] == $pid) {
            $v['url'] = get_url_list($v);
            $arr[] = $v;
            $arr = array_merge($arr, get_childs($cate, $v['id']));
        }
    }
    return $arr;
}


/**
 * 传递一个子分类ID返回所有的父级分类
 * @param $cate
 * @param $id
 * @return array
 */
function get_parents($cate, $id)
{
    $arr = array();
    foreach ($cate as $v) {
        if ($v['id'] == $id) {
            $arr[] = $v;
            $arr = array_merge(get_parents($cate, $v['pid']), $arr);
        }
    }
    return $arr;
}

// 获取单页地址
function get_url_page($v)
{
	$v['url'] = url('page', ['name' => $v['name']]);
    return $v['url'];
}


// 获取列表链接地址
function get_url_list($v)
{
    //判断是否直接跳转
    if (trim($v['url']) == '') {
		$v['url'] = url('index', ['name' => $v['name']]);
    }
    return $v['url'];
}

// 获取详情URL
function get_url_show($v)
{
    if ($v) {

		$cate = \think\facade\Db::name('category')->field('id,name')->where('id', $v['category_id'])->find();

        $url = url('show', ['name' => $cate['name'],'id' => $v['id']]);

    }
    return $url;
}

